package com.yunxi.service.localService.finance.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yunxi.bizToBaseData.pojo.VehiclePriceReqVO;
import com.yunxi.core.service.ServiceImplPlus;
import com.yunxi.core.util.WgrDate;
import com.yunxi.dao.finance.VehiclePriceMapper;
import com.yunxi.finance.pojo.DiscountVO;
import com.yunxi.finance.pojo.VehiclePriceQueryReqVO;
import com.yunxi.finance.pojo.VehiclePriceQueryRespVO;
import com.yunxi.finance.pojo.VehiclePriceVO;
import com.yunxi.hubToBaseData.pojo.SapVehiclePriceVo;
import com.yunxi.model.finance.VehiclePrice;
import com.yunxi.order.pojo.OrderVehicleVO;
import com.yunxi.service.localService.finance.VehiclePriceService;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 车辆价格 服务实现类
 * </p>
 *
 * @author 
 * @since 2019-05-22
 */
@Service
public class VehiclePriceServiceImpl extends ServiceImplPlus<VehiclePriceMapper, VehiclePrice> implements VehiclePriceService {

    @Override
    public void check(VehiclePriceVO vehiclePriceVO) {
        baseMapper.check(vehiclePriceVO);
    }

    @Override
    public VehiclePriceReqVO getDiscount(VehiclePriceReqVO ovv) {
        //1 通过条件排列组合查询 返回 折扣类型 折扣金额 或者比例
        ovv.setCurrDate(WgrDate.getCurrentTime("yyyyMMdd"));
        return SqlHelper.getObject(baseMapper.getDiscount(ovv));

        //查询是否有渠道商 订单号 定制接口
    }

    /**
     * 整车价格管理与查询
     * @param vo
     * @param pageIndex
     * @param pageSize
     * @return
     */
    @Override
    public PageInfo<VehiclePriceQueryRespVO> queryVehiclePriceResult(VehiclePriceQueryReqVO vo,int pageIndex,int pageSize){
        PageHelper.startPage(pageIndex,pageSize);
        List<VehiclePriceQueryRespVO> respVO = baseMapper.queryVehiclePriceResult(vo);
        PageInfo<VehiclePriceQueryRespVO> pageInfo = new PageInfo<>(respVO);
        return pageInfo;
    }


    /**
     * 计算整车价格
     * @param vo
     * @return
     */
    @Override
    public BigDecimal compoutePriceByCondition(VehiclePriceQueryReqVO vo){

        BigDecimal totalPrice = new BigDecimal(0);

        //
        //整车零售价格
        Date currentDate = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String currentDateStr = simpleDateFormat.format(currentDate);
        QueryWrapper qw = new QueryWrapper();
        qw.eq("PRODUCT_CODE",vo.getProductCode());
        qw.le("EFFECT_DATE",currentDateStr);
        qw.ge("VALI_DATE",currentDateStr);
        qw.eq("PRICE_TYPE_CODE","ZPR0");
        List<VehiclePrice> carVehiclePrices = baseMapper.selectList(qw);

        if(carVehiclePrices.size() > 0 ){
            totalPrice = totalPrice.add(carVehiclePrices.get(0).getAmount());
        }

        if(vo.getCustomPackCodes() != null) {
            //
            //选装包价格
            String[] featureCodes = vo.getCustomPackCodes().split(",");
            for (String item : featureCodes) {
                //
                //选装包价格
                QueryWrapper qwPack = new QueryWrapper();
                qwPack.eq("PRODUCT_CODE", vo.getProductCode());
                qwPack.le("EFFECT_DATE", currentDateStr);
                qwPack.ge("VALI_DATE", currentDateStr);
                qwPack.eq("PRICE_TYPE_CODE", "ZVAR");
                qwPack.eq("FEATURE_CODE", item);
                List<VehiclePrice> packVehiclePrices = baseMapper.selectList(qwPack);
                if (packVehiclePrices.size() > 0) {
                    totalPrice = totalPrice.add(packVehiclePrices.get(0).getAmount());
                }

            }
        }
        //
        //国补价格
        QueryWrapper qwGuoBu = new QueryWrapper();
        qwGuoBu.eq("PRODUCT_CODE",vo.getProductCode());
        qwGuoBu.eq("PRICE_TYPE_CODE","ZD05");
        qwGuoBu.le("EFFECT_DATE", currentDateStr);
        qwGuoBu.ge("VALI_DATE", currentDateStr);
        List<VehiclePrice> guoBuVehiclePrices = baseMapper.selectList(qwGuoBu);

        if(guoBuVehiclePrices.size() > 0){
            totalPrice = totalPrice.subtract(guoBuVehiclePrices.get(0).getAmount().abs());
        }



        return totalPrice;
    }

    @Override
    public BigDecimal queryPriceByCatalogCode(String catalogCode){
        Date currentDate = new Date();
        QueryWrapper qw = new QueryWrapper();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String currentDateStr = simpleDateFormat.format(currentDate);
        qw.eq("PRODUCT_CODE",catalogCode);
        qw.le("EFFECT_DATE",currentDateStr);
        qw.ge("VALI_DATE",currentDateStr);
        qw.eq("PRICE_TYPE_CODE","ZPRR");
        List<VehiclePrice> carVehiclePrices = baseMapper.selectList(qw);
        if(carVehiclePrices.size() > 0){
            return carVehiclePrices.get(0).getAmount();
        }else{
            return new BigDecimal(0);
        }

    }

    /**
     * 验证SAP传入价格有效期
     * @param sapVehiclePriceVo
     * @return
     */
    @Override
    public List<VehiclePrice> checkPriceValiddate(SapVehiclePriceVo sapVehiclePriceVo) {

        return  baseMapper.checkPriceValiddate(sapVehiclePriceVo);
    }

}
